package com.restaurant.application.score;

import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.score.support.AbstractScore;
import com.restaurant.application.score.support.ScoreData;
import com.restaurant.application.scoreset.ScoreSet;
import com.restaurant.application.shop.Shop;
import com.restaurant.application.shop.support.ShopVo;
import com.restaurant.application.shoplist.ShopList;

import java.util.List;
import java.util.stream.Collectors;

public class ScoreImpl extends AbstractScore {
    @Override
    public ViewUpdate save(ScoreData save, int[] seq) {
        if (getData().getId() != null) {
            save.withId(getData().getId()).withCreateTime(getData().getCreateTime());
        } else {
            save.withId(SnowFlake.findUniqueId()).withCreateTime(DateTimeUtils.getCurrentMillisecond());
        }
        UID uid = getSingleton(ShopList.class).findShopByName(save.getShopName());
        String id = getUnit(Shop.class, uid).findData().getId();
        save.withShop(uid).withShopId(id);
        save.withModifyTime(DateTimeUtils.getCurrentMillisecond());
        setData(save);
        getSingleton(ScoreSet.class).addScore(getId());
        return returnPopFromContainer(getId(), false);
    }

    @Override
    public ViewUpdate cancel(int[] seq) {
        return returnPopFromContainer(getId(), false);
    }

    @Override
    public View getFullView() {
        PageForm pageForm = newFormShell(getData().duplicate()).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }

    @Override
    public View getViewLead(ViewSpec spec) {
        return null;
    }

    @Override
    public ScoreData findData() {
        return getData().duplicate();
    }

    private FormShell newFormShell(ScoreData scoreData) {

        FormShell formShell = new FormShell("积分比例", 3, 3);

        formShell.source(scoreData, DataModes.VALIDATE);
        List<ShopVo> vos = getSingleton(ShopList.class).findAllShopData();
        List<String> collect = vos.stream().map(ShopVo::getName).collect(Collectors.toList());
        formShell.at(0, 0, "店名")
                .getEntry(ScoreData.FN_SHOPNAME, "")
                .selectFrom(collect)
                .withValue(scoreData.getShopName())
                .notNull()
                .getContent().setEditable(true);

        formShell.at(1, 0, "消费金额")
                .getEntry(ScoreData.FN_PAYING, scoreData.getPaying() == null ? "" : String.valueOf(scoreData.getPaying()))
                .placeHolder("")
                .simpleEntry(EntryTypes.LONG)
                .getContent().setEditable(true);

        formShell.at(2, 0, "所得积分")
                .getEntry(ScoreData.FN_SCORE, scoreData.getScore() == null ? "" : String.valueOf(scoreData.getScore()))
                .placeHolder("")
                .simpleEntry(EntryTypes.LONG)
                .getContent().setEditable(true);

        formShell.at(3, 0, "保存")
                .buttonField(getPath(CMD_SAVE), "", true);

        formShell.at(3, 1, "取消")
                .buttonField(getPath(CMD_CANCEL), "", false);

        return formShell;
    }
}
